﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication.MessageContent
{
    public class MessageContainer<T>:List<T>
    {
        /// <summary>
        /// 最大记录条数（保留尾数），如果小于或者等于0则不限制
        /// </summary>
        public int MaxReocrdCount { get; set; }
        private MessageContainer()
        {
        }

        public MessageContainer(int maxRecordCount)
        {
            MaxReocrdCount = maxRecordCount;
        }

        new public void Add(T item)
        {
            base.Add(item);
            RemoveExpressItems();
        }

        new public void AddRange(IEnumerable<T> collection)
        {
            base.AddRange(collection);
            RemoveExpressItems();
        }

        new public void Insert(int index,T item)
        {
            base.Insert(index,item);
            RemoveExpressItems();
        }

        new public void InsertRange(int index, IEnumerable<T> collection)
        {
            base.InsertRange(index, collection);
            RemoveExpressItems();
        }

        private void RemoveExpressItems()
        {
            if (MaxReocrdCount > 0 && base.Count > MaxReocrdCount)
            {
                base.RemoveRange(0, base.Count - MaxReocrdCount);
            }
        }
    }
}